﻿package com.hutra.utils {
	import flash.utils.*;
	import flash.net.*;
	import flash.events.*;
	import flash.display.*;
	import flash.geom.Point;
	
	/**
	 * the package on site http://www.libspark.org/wiki/WikiStart/en
	 * modifined by hunglx
	 * function modifine is "multiLoad" add object "function:Function" call "_function(this._loaders[i])"
	 */
	public class Loaderloader extends EventDispatcher {
		public var msloading:MovieClip;
		public var _loaders:Array = new Array();
		public var settimeout:uint;
		public var m:int = 0;
		
		public function Loaderloader() {
		}
		
		public static function convert(evt:Event):DisplayObject {
			var loader:Loader = (Loader)(evt.target.loader);
			return loader.content;
		}
		
		public function miniLoader(string:String, onCompleteMini:Function) {
			var loader:Reloader = new Reloader();
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteMini);
			var byte:ByteArray = Base64.decodeToByteArray(string);
			loader.loadBytes(byte);
		}
		
		public function addTarget(objbinary:ByteArray, namearray:String, functioncall:Function):void {
			try {
				var loader:Reloader = new Reloader();
				loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event) {
						onCompleteTarget(namearray, e);
					});
				loader._parent = this;
				loader._accept = functioncall;
				loader._target = objbinary;
				loader._targettext = namearray;
				loader._index = this._loaders.push(loader) - 1;
			} catch (e:Error) {
				trace("Error Loaderloader addTarget");
			}
		}
		
		public function multiLoad():void {
			try {
				if (this._loaders.length == 0)
					this.dispatchEvent(new Event(Event.COMPLETE));
				for (var i:int = 0; i < this._loaders.length; i++) {
					try {
						this._loaders[i].loadBytes(this._loaders[i]._target);
					} catch (e:Error) {
						trace("Error multiLoads: " + i + " | " + this._loaders[i]._targettext);
					}
				}
			} catch (e:Error) {
				trace("Error multiLoad");
			}
			//settimeout = setInterval(nextLoadder,5.000000E-001);
		}
		
		public function nextLoadder():void {
			m++;
			if (m >= this._loaders.length || this._loaders.length == 0) {
				m = 0;
				this.dispatchEvent(new Event(Event.COMPLETE));
				clearInterval(settimeout);
			} else {
				this._loaders[m].loadBytes(this._loaders[m]._target);
			}
		}
		
		public function setRound():void {
			try {
				var i:int = m;
					//msloading.txt.text=""; 
					//msloading.txt.text = ""+ Math.round((i/this._loaders.length)*100)+""; 
					//msloading.bg2.mask = msloading.pMask;
					//paintLoader(350,Math.round((i/this._loaders.length)*100));
			} catch (e:Error) {
			}
		}
		
		public function paintLoader(param1:int, param2:int) {
			msloading.bg2.visible = true;
			msloading.bg1.visible = true;
			var _loc_3:int = 0;
			var _loc_4:Point = null;
			var _loc_5:int = 0;
			var _loc_6:int = 0;
			_loc_3 = 360 * param2 / 100;
			msloading.pMask.graphics.clear();
			_loc_4 = new Point(param1 * Math.sin(_loc_3 * Math.PI / 180), (-param1) * Math.cos(_loc_3 * Math.PI / 180));
			if (_loc_4.x < 0) {
				_loc_5 = -1;
			} else {
				_loc_5 = 1;
			}
			if (_loc_4.y < 0) {
				_loc_6 = -1;
			} else {
				_loc_6 = 1;
			}
			msloading.pMask.graphics.beginFill(0);
			msloading.pMask.graphics.moveTo(0, -param1);
			msloading.pMask.graphics.lineTo(0, 0);
			msloading.pMask.graphics.lineTo(_loc_4.x, _loc_4.y);
			msloading.pMask.graphics.lineTo(_loc_5 * param1, _loc_4.y);
			msloading.pMask.graphics.lineTo(_loc_5 * param1, param1);
			msloading.pMask.graphics.lineTo(param1, param1);
			msloading.pMask.graphics.lineTo(param1, -param1);
			msloading.pMask.graphics.endFill();
			return;
		} // end function
		
		/**
		 * function Complete
		 * @param	i_loader
		 */
		public function Completes(i_loader:Reloader):void {
			try {
				i_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onCompleteTarget);
				this._loaders[i_loader._index] = null;
			} catch (e:Error) {
				trace("Error Completes");
			}
		}
		
		/**
		 * is return true||false
		 * @return
		 */
		public function isComplete():Boolean {
			try {
				for (var i:int = 0; i < this._loaders.length; i++) {
					if (this._loaders[i] != null) {
						return false;
					}
				}
			} catch (e:Error) {
				trace("Error isComplete");
				return false;
			}
			return true;
		}
		
		/**
		 * onCompleteTarget
		 * @param	namearray
		 * @param	e
		 */
		public function onCompleteTarget(namearray:String = null, e:Event = null):void {
			m++;
			setRound();
			try {
				var loader:Reloader = (Reloader)(e.target.loader);
				loader._accept(namearray, e.currentTarget);
				loader._parent.Completes(loader);
				if (loader._parent.isComplete()) {
					loader._parent.dispatchEvent(new Event(flash.events.Event.COMPLETE));
						//msloading.bg2.visible = false;
						//msloading.bg1.visible = false;
				}
			} catch (e:Error) {
				trace("Error onCompleteTarget");
			}
		}
	}
}
import flash.utils.*;
import flash.net.*;
import flash.events.*;
import flash.display.*;
import com.hutra.utils.Loaderloader;

class Reloader extends Loader {
	public var _parent:Loaderloader;
	public var _index:int;
	public var _accept:Function;
	public var _target:ByteArray;
	public var _targettext:String;
}